using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_Profile : admin_BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ucPerInfo.LoadData();
    }
    protected void btnUpdateInfo_Click(object sender, EventArgs e)
    {
        User user = Session["user"] as User;
        if (user == null)
        {
            ShowMessage("Error. Profile cannot be loaded");
            return;
        }
        PersonDAO daoPerson = new PersonDAO();
        Person per = new Person();

        per = user.PersonInfor;
        per.FirstName = ucPerInfo.FirstName;
        per.LastName = ucPerInfo.LastName;
        per.Address = ucPerInfo.Address;
        per.Gender = ucPerInfo.Gender;
        per.City = ucPerInfo.City;
        per.State = ucPerInfo.State;
        per.Country = ucPerInfo.Country;
        per.Email = ucPerInfo.Email;
        per.DateOfBirth = ucPerInfo.DateOfBirth;
        per.Phone = ucPerInfo.Phone;
        if (daoPerson.Update(per) > 0)
        {
            ShowMessage("Profile is updated");            
            Session["user"] = user;
        }
        else ShowMessage("Error. Change cannot be saved.");
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        this.Page.Validate();
        if (!this.Page.IsValid) return;
        UserDAO daoUser = new UserDAO();
        User user = Session["user"] as User;
        if (user == null)
        {
            ShowMessage("Error. Action failed");
            return;
        }
        if (daoUser.ChangePassword(user.UserName,CypherTextUtil.MD5Encrypt(txtCurrentPass.Text),CypherTextUtil.MD5Encrypt(txtNewPass.Text)) > 0)
        {
            ShowMessage("Password has been changed");
        }
        else ShowMessage("Error. Password cannot be changed.");
    }
}
